package org.luxor.commons.websocket;


import org.luxor.commons.core.utils.SpringContextUtils;
import org.luxor.commons.websocket.component.session.WsSessionStorage;

import javax.websocket.Session;

/**
 * WebSocket Session 上下文工具类
 *
 * @author Mr.yan  @date 2021/9/5
 */
public class WsSessionContextUtils {

    private volatile static WsSessionStorage storage = null;

    private static WsSessionStorage getStorage() {
        if (storage == null) {
            synchronized (WsSessionContextUtils.class) {
                if (storage == null) {
                    storage = SpringContextUtils.getBean(WsSessionStorage.class);
                    return storage;
                }
            }
        }
        return storage;
    }

    /**
     * 添加会话
     *
     * @param token
     * @param session
     */
    public static void addSession(String token, Session session) {
        getStorage().addSession(token, session);
    }

    /**
     * 移除会话
     *
     * @param token
     */
    public static void removeSession(String token) {
        getStorage().removeSession(token);
    }

    /**
     * 发送消息
     *
     * @param token
     * @param message
     */
    public static void send(String token, String message) {
        getStorage().send(token, message);
    }

    /**
     * 群发消息
     *
     * @param message
     */
    public static void sendAll(String message) {
        getStorage().sendAll(message);
    }

}
